//
//  VCCompanyManger.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/2/16.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCCompanyManger: VCBase {
    
    private lazy var companyBodyView:CompanyBodyView = {
        let ui = CompanyBodyView()
        ui.delegate = self
        ui.backgroundColor = UIColor.white
        let uiY = realHeight + MARGIN10
        ui.frame = CGRect(x: 0, y: uiY, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - uiY)
        return ui
    }()
    
    public var companyExpenseInfo:CompanyExpenseInfo?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_view()
        init_data()
    }   
}

// MARK: 逻辑部分
extension VCCompanyManger {
    
    // 保存报销
    func savePerson(bean:CallbackBean, isShowMore: Bool) -> Void {
        if bean.first?.count == 0 {
            self.show(text: "抬头不能为空")
            return
        }
        if bean.second?.count == 0 {
            self.show(text: "税号不能为空")
            return
        }
        if isShowMore {
            if bean.three?.count == 0 {
                self.show(text: "公司地址不能为空")
                return
            }
            if bean.four?.count == 0 {
                self.show(text: "公司电话不能为空")
                return
            }
            if bean.five?.count == 0 {
                self.show(text: "银行名称不能为空")
                return
            }
            if bean.sex?.count == 0 {
                self.show(text: "银行账号不能为空")
                return
            }
        }
        guard let userid = UserDefaults.standard.value(forKey: SP_USERID) else {
            self.show(text: "本地数据获取失败，请使用账号密码重新登录")
            return
        }
        // 拼接请求
        let request = NSMutableDictionary()
        request.setValue(userid, forKey: "userid")
        request.setValue(bean.first, forKey: "title")
        request.setValue(bean.second, forKey: "duty")
        if isShowMore {
            request.setValue(bean.five, forKey: "bankname")
            request.setValue(bean.sex, forKey: "bankaccount")
            request.setValue(bean.four, forKey: "compantmobile")
            request.setValue(bean.three, forKey: "companyaddress")
        }
        // 请求地址
        var url = "add/company/expense"
        if let company = companyExpenseInfo {
            url = "edit/company/expense"
            request.setValue(company.companyexpenseid, forKey: "companyexpenseid")
        }
        // 请求
        self.doPost(api: url, dict: request) { (dict) in
            self.show(text: "保存成功")
            self.navigationController?.popViewController(animated: true)
        }
    }
    
    // 显示二维码图片
    @objc func showQrCode() -> Void {
        guard let company = companyExpenseInfo else {
            return
        }
        let vc = VCQrCode()
        vc.companyName = company.title
        vc.message = company.description
        self.pushVc(title: "我的二维码", vc: vc)
    }
}


// MARK: 代理
extension VCCompanyManger:CompanyBodyViewDelegate {
    
    func company_save(item: CallbackBean, isShowMore: Bool) {
        self.savePerson(bean: item, isShowMore: isShowMore)
    }
    
    func company_delete(company: CompanyExpenseInfo, isShowMore: Bool) {
        // 拼接数据
        let request = NSMutableDictionary()
        request.setValue(company.companyexpenseid, forKey: "companyexpenseid")
        // 请求
        self.doPost(api: "delete/company/expense", dict: request) { (dict) in
            self.show(text: "删除成功")
            self.navigationController?.popViewController(animated: true)
        }
    }
}

// MARK: 初始化
extension VCCompanyManger {
    
    private func init_data() -> Void {
        guard let company = companyExpenseInfo else {
            return
        }
        // 回显数据
        companyBodyView.companyExpenseInfo = company
        // 显示二维码按钮
        let item = UIBarButtonItem(title: "二维码", style: .done, target: self, action: #selector(showQrCode))
        item.tintColor = UIColor.black
        navigationItem.rightBarButtonItem = item
    }
    
    private func init_view() -> Void {
        view.addSubview(backroundWhite)
        view.addSubview(companyBodyView)
    }
}
